/**
 * 
 */
package com.kingdee.eas.minghua.common;

import java.awt.Color;
import java.math.BigDecimal;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.ctrl.extendcontrols.KDBizPromptBox;
import com.kingdee.bos.ctrl.kdf.table.IColumn;
import com.kingdee.bos.ctrl.kdf.table.KDTDefaultCellEditor;
import com.kingdee.bos.ctrl.kdf.table.KDTable;
import com.kingdee.bos.ctrl.kdf.util.style.Styles;
import com.kingdee.bos.ctrl.swing.KDDatePicker;
import com.kingdee.bos.ctrl.swing.KDFormattedTextField;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.entity.SelectorItemInfo;
import com.kingdee.eas.basedata.assistant.CurrencyFactory;
import com.kingdee.eas.basedata.assistant.CurrencyInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.common.client.SysContext;
import com.kingdee.eas.fi.fa.manage.client.FaClientUtils;

/**
 * @author Administrator
 * 
 */
public class CellEditorHelper {
	
	public static String DATE_FORMAT = "%{yyyy-MM-dd}t";
	public static String NUMB_FORMAT = "$number$";
//	public static String AMOU_FORMAT = "#,##0.##";
	public static String AMOU_FORMAT = "%r{#,##0.00}f"; 
	public static BigDecimal MAX_SIZE = FaClientUtils.MAX_DECIMAL;
	public static Color colorAudit = new Color(0xE3, 0xEF, 0xDE);
	public static Color colorSubTotal = new Color(0xF7,0xFF,0x78);
	public static Color colorTotal = new Color(0xFF, 0xC3, 0x10);


	public static void f7CellEditor(KDTable table, String columnName, String query) {
		IColumn column = table.getColumn(columnName);
		KDBizPromptBox bizPromptBox = new KDBizPromptBox();
		bizPromptBox.setQueryInfo(query);
		bizPromptBox.setVisible(true);
		bizPromptBox.setEditable(true);
		bizPromptBox.setDisplayFormat(NUMB_FORMAT);
		bizPromptBox.setEditFormat(NUMB_FORMAT);
		bizPromptBox.setCommitFormat(NUMB_FORMAT);
		column.setEditor(new KDTDefaultCellEditor(bizPromptBox));
	}

	public static void dateCellEditor(KDTable table, String columnName) {
		IColumn column = table.getColumn(columnName);
		column.setEditor(new KDTDefaultCellEditor(new KDDatePicker()));
		column.getStyleAttributes().setNumberFormat(DATE_FORMAT);
	}

	public static void currencyCellEditor(KDTable table, String columnName) throws Exception {
		IColumn column = table.getColumn(columnName);
		KDFormattedTextField formattedTextField = new KDFormattedTextField(KDFormattedTextField.DECIMAL);
		
		formattedTextField.setPrecision(getCurrencyScale());
		formattedTextField.setMaximumValue(MAX_SIZE);
		formattedTextField.setNegatived(true);
		formattedTextField.setHorizontalAlignment(KDFormattedTextField.RIGHT);
		column.setEditor(new KDTDefaultCellEditor(formattedTextField));
		column.getStyleAttributes().setHorizontalAlign(
				Styles.HorizontalAlignment.RIGHT);
		column.getStyleAttributes().setNumberFormat(AMOU_FORMAT);
	}

	private static int getCurrencyScale() throws EASBizException, BOSException {
		int currencyScale = -1;
		if (currencyScale == -1) {
			SelectorItemCollection sic = new SelectorItemCollection();
			sic.add(new SelectorItemInfo("id"));
			sic.add(new SelectorItemInfo("precision"));
			ObjectUuidPK pk = new ObjectUuidPK(SysContext.getSysContext()
					.getCurrentFIUnit().getBaseCurrency().getId());
			CurrencyInfo currencyInfo = CurrencyFactory.getRemoteInstance()
					.getCurrencyInfo(pk, sic);
			currencyScale = currencyInfo.getPrecision();
		}
		return currencyScale != -1 ? currencyScale : 2;
	}
}
